﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Cards.Objects;
using Cards.Util;

namespace Cards.GUI
{
    class INS_BlackJackPlayer : INS_Player
    {
        int m_index;
        Boolean m_isBanker;

        public int INDEX
        {
            get { return m_index; }
            set { m_index = value; }
        }

        public Boolean BANKER
        {
            get { return m_isBanker; }
            set { m_isBanker = value; }
        }

        public INS_BlackJackPlayer(int _x, int _y, int _no, String _name, Image _picPath,
                            INS_Stage _stage, PLAYER_TYPE _playerkind, int _width, int _height,
                            int _xStart, int _yStart, int _numofcards, int _maxnumofcards)
                                : base(_x, _y, _no, _name, _picPath, _stage, _playerkind, _width, _height,
                                        _xStart, _yStart, _numofcards, _maxnumofcards)
        {
            if (_playerkind == 0)
            {
                m_player = new CBlackJackPlayer(_name, this);
            }
            else if (_playerkind == PLAYER_TYPE.COMPUTER)
            {
                m_player = new CBlackJackPlayer(_name, this);
            }
            m_index = 0;
            m_isBanker = false;
        }

        public override void Init()
        {
            this.Size = new Size(new Point(m_width, m_height));
            this.Location = new Point(x, y);
            for (int i = 0; i < m_maxNumOfCards; i++)
            {
                m_cardsArray[i] = new INS_Card(Constants.s_avatarWidth + Constants.s_posx_player + xStart
                                                    + i * Constants.s_space_bw_two_cards,
                                                  Constants.s_posy_player + yStart,// + Constants.s_alphay,
                                                  -1, null,
                                                  Constants.s_cardWidth, Constants.s_cardHeight, this);
            }
        }

        public void InitCardsPosition()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                m_cardsArray[i].X = Constants.s_avatarWidth + Constants.s_posx_player + xStart
                                                    + i * Constants.s_space_bw_two_cards;
                m_cardsArray[i].Y = Constants.s_posy_player + yStart;
            }
        }

        public void ResetCardsState()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                m_cardsArray[i].CARD.PLAY = false;
                m_cardsArray[i].SELECT = -1;
            }
        }

        public void DeleteCardsArray()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                m_cardsArray[i].Dispose();
                m_cardsArray[i] = null;
            }
            m_numOfCards = Constants.BLACKJACK_NUMOFCARDS;
        }

        public void ResetPlayer()
        {
            m_player.WINPOS = 0;
            m_player.NOTETOADD = "";
            m_player.NOTETOSUBTRACT = "";
            m_player.AMOUNT_DECREASE_MONEY = 0;
            m_player.WINMONEY = 0;
            m_player.AMOUNT_INCREASE_MONEY = 0;
            m_player.NOTMOVE = true;
            m_player.SKIP = false;
            m_player.WIN = false;
            lState.Text = "";
        }

        public void CreateGUI()
        {
            this.m_pic = new PictureBox();
            m_pic.Size = new Size(Constants.s_avatarWidth, Constants.s_avatarHeight);
            m_pic.Location = new Point(xStart, yStart);

            lState.Size = new Size(Constants.s_avatarWidth, 20);
            lState.Location = new Point(xStart, yStart + Constants.s_avatarHeight);
            lState.ForeColor = Color.Blue;
            this.Controls.Add(lState);

            lAmountOfMoney.Size = new Size(Constants.s_avatarWidth, 20);
            lAmountOfMoney.Location = new Point(xStart, yStart + Constants.s_avatarHeight + 20);
            lAmountOfMoney.ForeColor = Color.Blue;
            lAmountOfMoney.Text = m_player.MONEY.ToString();
            lAmountOfMoney.ForeColor = Color.Brown;
            this.Controls.Add(lAmountOfMoney);
            
            if (BANKER)
            {
                lTitle.Size = new Size(Constants.s_avatarWidth, 20);
                lTitle.Location = new Point(xStart, yStart + 150);
                lTitle.ForeColor = Color.Blue;
                this.Controls.Add(lTitle);
                lTitle.Text = "Nhà Cái";
                Font font = new Font(lTitle.Font, FontStyle.Bold);
                lTitle.Font = font;
            }

            if (!Environment.CurrentDirectory.Equals(MainFrame.sCurDir))
            {
                Environment.CurrentDirectory = MainFrame.sCurDir;
            }
            //Bitmap bmp = new Bitmap(m_picPath);
            //m_pic.Image = (Bitmap)bmp;
            m_pic.Image = m_picPath;
            m_pic.SizeMode = PictureBoxSizeMode.StretchImage;
            this.Controls.Add(m_pic);

            CreateCardGUI();
        }

        public void CreateGUIToReplay()
        {
            CreateCardGUI();
        }

        public void CreateCardGUI()
        {
            AssignCardPictures();

            for (int i = 0; i < m_numOfCards; i++)
            {
                m_cardsArray[i].CreateGUI();
                m_cardsArray[i].Visible = false;
                this.Controls.Add(m_cardsArray[i]);
                m_cardsArray[i].BringToFront();
            }

            if (m_playerKind == PLAYER_TYPE.COMPUTER)
            {
                if (m_gPic == null)
                {
                    m_gPic = new PictureBox[m_maxNumOfCards];
                }

                for (int i = 0; i < m_maxNumOfCards; i++)
                {
                    if (m_gPic[i] == null)
                    {
                        m_gPic[i] = new PictureBox();
                    }
                    m_gPic[i].Size = new Size(Constants.s_cardWidth, Constants.s_cardHeight);
                    if (!Environment.CurrentDirectory.Equals(MainFrame.sCurDir))
                    {
                        Environment.CurrentDirectory = MainFrame.sCurDir;
                    }

                    m_gPic[i].Image = global::Cards.Properties.Resources.bobai2;
                    m_gPic[i].SizeMode = PictureBoxSizeMode.StretchImage;
                    m_gPic[i].Location = new Point(Constants.s_avatarWidth + Constants.s_posx_player + xStart
                                                                               + i * Constants.s_space_bw_two_cards,
                                                   Constants.s_posy_player + yStart);
                }

                for (int i = 0; i < m_numOfCards; i++)
                {
                    m_gPic[i].Visible = false;
                    this.Controls.Add(m_gPic[i]);
                    m_gPic[i].BringToFront();
                }

                // Comment/uncomment if you want to display all cards of all players
                HideCards();
                //DisplayCards();
            }
        }

        public override void ReceiveTurn()
        {
            if (!INS_Stage.sPlay)
            {
                return;
            }

            base.ReceiveTurn();

            if (m_playerKind == PLAYER_TYPE.COMPUTER)
            {
                sleepTimer.Start();
                sleepTimer.Enabled = true;
            }
        }

        public override void EndTurn()
        {
            base.EndTurn();
            SetStateText("Đã xong");
        }

        public void SkipPlaying()
        {
            SetStateText("Đã xong");
        }

        protected override void sleepTimer_Tick(object sender, EventArgs e)
        {
            m_sleep++;
            if (m_sleep == 1)
            {
                m_sleep = 0;
                sleepTimer.Stop();
                ((CBlackJackStage)this.INSSTAGE.STAGE).TakeCard();
            }
        }

        public Boolean TakeACard()
        {
            if (this.INDEX >= this.MAXNUMOFCARDS)
            {
                return false;
            }

            m_cardsArray[m_index].CARD.CARDTYPE = this.INSSTAGE.IPC.PC[this.INSSTAGE.IPC.PC.INDEX];
            m_cardsArray[m_index].CARD.CreateCardValue();
            this.INSSTAGE.IPC.PC.INDEX++;

            AssignCardPictures();

            m_cardsArray[m_index].CreateGUI();

            if (m_playerKind == PLAYER_TYPE.COMPUTER ||
                ((MainFrame.s_multiType != MULTI_PLAYER_TYPE.SINGLE_PLAYER) && (m_playerKind == PLAYER_TYPE.HUMAN) &&
                 (m_No != MainFrame.m_frmMultiGame.GAMEPLAYERID - 1)))
            {
                this.Controls.Add(m_gPic[m_index]);
                m_gPic[m_index].BringToFront();
                m_cardsArray[m_index].Visible = false;
                m_gPic[m_index].Visible = false;
            }
            else
            {
                m_cardsArray[m_index].Visible = false;
            }

            this.Controls.Add(m_cardsArray[m_index]);
            m_cardsArray[m_index].BringToFront();

            m_index++;
            m_numOfCards++;

            INSSTAGE.OneCardDistributionEffect(m_No, m_index - 1);

            return true;
        }

        public override void CalculateMoney(long amount)
        {
            m_player.CalculateMoney(amount);
        }
    }
}
